﻿namespace YunQue.Common.Helpers
{
    /// <summary>
    /// RAS 加密 解密
    /// </summary>
    public class RASEncryption
    {
        /// <summary>
        /// 公钥
        /// </summary>
        private const string _publicKey = @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxs4GlqjYbvvTWQ40dQuu
XjAqIkzDMLO64YkU+F/VDzf947cVZjEztCQtTLCfvKtumYmrpQeD4Z2AxAnTtBhl
kv9jTy7OkPDWEC9gR9fuet1f+4b8ch94XWTWhZTTXJM1i89bAFCOqvkkKsggekKr
WDOChFQKT4kH0NoUbrkhVwtqJDRhnDA8tq3A3fYbsPrkoSZE91cpnDY9mW/JmyvG
sayL2MGbXyfGXZLaDDmscTFahFMuCwzv0LBJKMtl7E+X1RyNoynCupymn3LD2+oy
iYekXVc88mcOOMU1uDuTijIOtPDAwiRa8CWTiAa2jA9kpOFUYu2kWyohtS3D8baC
BwIDAQAB";
        /// <summary>
        /// 私钥
        /// </summary>
        private const string _privateKey = @"MIIEpAIBAAKCAQEAxs4GlqjYbvvTWQ40dQuuXjAqIkzDMLO64YkU+F/VDzf947cV
ZjEztCQtTLCfvKtumYmrpQeD4Z2AxAnTtBhlkv9jTy7OkPDWEC9gR9fuet1f+4b8
ch94XWTWhZTTXJM1i89bAFCOqvkkKsggekKrWDOChFQKT4kH0NoUbrkhVwtqJDRh
nDA8tq3A3fYbsPrkoSZE91cpnDY9mW/JmyvGsayL2MGbXyfGXZLaDDmscTFahFMu
Cwzv0LBJKMtl7E+X1RyNoynCupymn3LD2+oyiYekXVc88mcOOMU1uDuTijIOtPDA
wiRa8CWTiAa2jA9kpOFUYu2kWyohtS3D8baCBwIDAQABAoIBAB91zP8cexudCEYI
hG6dSZuoHZLN+PmFB6bXLZVcfPfqkWfuuhkw4hC4xN5k+PI/YsIzUhFMjicu6Iid
UX4nejN37LxEGPZA1m87QA4+coPI8n54Twy0RDTrVzNOaOA2f3u/dZQoK1uND5ch
klomoLzVcesE7ZjGg5EeYatf6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7tWsgqOsA==";
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Encrypt(string content)
        {
            using (var rsa = RSA.Create())
            {
                string base64Key = _publicKey.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\n", "").Replace("\r", "");
                // 导入公钥
                rsa.ImportRSAPublicKey(Convert.FromBase64String(base64Key), out _);
                // 加密数据
                byte[] encryptedBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), RSAEncryptionPadding.Pkcs1);
                return Convert.ToBase64String(encryptedBytes);
            }
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Decrypt(string content)
        {
            using (var rsa = RSA.Create())
            {
                string base64Key = _privateKey.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\n", "").Replace("\r", "");
                // 导入私钥
                rsa.ImportRSAPrivateKey(Convert.FromBase64String(base64Key), out _);
                // 解密数据
                byte[] decryptedBytes = rsa.Decrypt(Convert.FromBase64String(content), RSAEncryptionPadding.Pkcs1);
                return Encoding.UTF8.GetString(decryptedBytes);
            }
        }
    }
}
